package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.appbar.MaterialToolbar;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.adapter.ViewPagerAdapter;

public class ViewPagerActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("ViewPager2 示例");
        }

        // 初始化ViewPager2
        viewPager = findViewById(R.id.view_pager);
        String[] data = {"页面 1", "页面 2", "页面 3", "页面 4", "页面 5"};
        adapter = new ViewPagerAdapter(data);
        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 